package com.herenit.order_model.service.impl;

import com.herenit.order_model.entity.OmFailureDetail;
import com.herenit.order_model.entity.OmRecord;
import com.herenit.order_model.model.Message;
import com.herenit.order_model.service.OmFailureDetailService;
import com.herenit.order_model.service.OmRecordService;
import com.herenit.order_model.utils.rabbitmq.RabbitMQUtils;
import com.herenit.order_model.service.RabbitMQService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

@Service
public class RabbitMQServiceImpl implements RabbitMQService {

    @Autowired
    private OmFailureDetailService omFailureDetailService;

    @Autowired
    private OmRecordService omRecordService;

    @Autowired
    private RabbitMQUtils rabbitMQUtils;

    @Override
    public void sendMsg(String institutionCode, String message) {
        rabbitMQUtils.send(institutionCode, message);
    }

    @Override
    @Transactional
    public Message consumeMsg(String institutionCode, String doctorCode) {
        OmRecord omRecord;
        OmFailureDetail omFailureDetail;
        // 模拟 判断该医生是否有访问该机构的权限
//        if (!DoctorHospitalMap.checkDoctorAndHospital(institutionCode, doctorCode)) {
//            return new Message("0", MessageConstant.NO_ACCESS_PERMISSION);
//        }
        // 调用工具类 进行消费
        Message message = rabbitMQUtils.consume(institutionCode, doctorCode);
        // 消息对象
        Map receive = (Map) message.getResult();
        // 从优先队列与普通队列获取消息对象为空 不需要对数据库进行操作 直接返回
        if (receive == null) {
            return message;
        }
        // 消息对象中的内容(字符串)
        String messageBody = (String) receive.get("messageBody");
        // 如果是从优先队列获取的就有在数据库中对应的Id值 如果一般队列获取则为null
        Long recordId = (Long) receive.get("recordId");
        // 0 代表处理失败
        if ("0".equals(message.getCode())) {
            if (recordId == null) {
                // 从一般队列获取 (模拟失败)
                // 记录到表中 状态为0
                omRecord = omRecordService.save(new OmRecord(institutionCode, doctorCode, 0, messageBody, 1));
                // 发送到打回优先队列中
                rabbitMQUtils.sendPrior(institutionCode, messageBody, omRecord.getId());
                // 新建失败细节
                omFailureDetail = new OmFailureDetail(omRecord.getId(), omRecord.getContent(), message.getMessage());
                // 插入失败细节表中
                omFailureDetailService.save(omFailureDetail);
            } else {
                // 从优先队列获取失败 (模拟失败)
                // 失败过 将count++ 再刷新回表中去
                omRecord = omRecordService.findById(recordId);
                omRecord.setCount(omRecord.getCount() + 1);
                omRecordService.save(omRecord);
                // 发送到打回优先队列中
                rabbitMQUtils.sendPrior(institutionCode, messageBody, omRecord.getId());
                // 新建失败细节
                omFailureDetail = new OmFailureDetail(omRecord.getId(), omRecord.getContent(), message.getMessage());
                // 插入失败细节表中
                omFailureDetailService.save(omFailureDetail);
            }
        } else if ("200".equals(message.getCode())) {
            if (recordId == null) {
                // 说明是从一般队列中获取成功的
                omRecordService.save(new OmRecord(institutionCode, doctorCode, 1, messageBody, 1));
            } else {
                // 说明是从优先队列中获取成功的
                omRecord = omRecordService.findById(recordId);
                omRecord.setCount(omRecord.getCount() + 1);
                omRecord.setStatus(1);
                omRecordService.save(omRecord);
            }
        }
        // 对数据库操作完成 返回
        return message;
    }
}
